#   Copyright 2006 Jerome Lacoste (jerome@coffeebreaks.org)
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
class Client < ActiveRecord::Base

  belongs_to :owner, :class_name => "User", :foreign_key => 'owner_id'
  has_many :projects
  belongs_to :tracker

#  has_many :worklogs, :through => :projects

  validates_presence_of :short, :name
  validates_uniqueness_of :short

  # FIXME url formatter ?
  # FIXME only if not null
  # could validate url using a validator (check that server exists)
  validates_format_of :url,
                      :with => %r{^http(s|):.+$}i,
                      :message => "must be a URL."

  def before_create
    self.short = self.short.upcase
  end

  def issue_tracker
    tracker.as_issue_tracker unless tracker.nil?
  end

  def issue_tracker_display
    return "none" if tracker.nil?
    tracker.name + " : " + tracker.url
  end
end
